<template>
  <div>
    <div style="display: flex;justify-content: space-between">
      <div>
        <el-input
            placeholder="请输入任务标题或内容进行搜索..."
            prefix-icon="el-icon-search"
            v-model="keyword"
            style="width: 350px;margin-right: 10px"
            @keydown.enter.native="initTasks"
            clearable
            :disabled="loading">
        </el-input>
        <el-button icon="el-icon-search" type="primary" @click="initTasks" :disabled="loading">搜索</el-button>
      </div>
      <div>
        <el-button type="success" @click="exportData" icon="el-icon-download" style="margin-right: 5px">
          导出数据
        </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="showAddTaskView">添加任务</el-button>
      </div>
    </div>

    <div style="margin-top: 10px">
      <el-table
          :data="tasks"
          stripe
          border
          style="width: 100%"
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading">
        <el-table-column
            type="selection"
            width="40">
        </el-table-column>
        <el-table-column
            prop="id"
            label="ID"
            width="50">
        </el-table-column>
        <el-table-column
            prop="title"
            label="任务标题"
            width="150">
        </el-table-column>
        <el-table-column
            prop="content"
            label="任务内容">
        </el-table-column>
        <el-table-column
            label="发布者"
            width="80"
            prop="hrName">
        </el-table-column>
        <el-table-column
            label="指派员工"
            width="120"
            prop="empName"
        >
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间"
            width="160">
        </el-table-column>
        <el-table-column
            prop="endTime"
            label="截止时间"
            width="160">
        </el-table-column>
        <el-table-column
            label="状态"
            width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '未分配'" type="info">{{ scope.row.status }}</el-tag>
            <el-tag v-else-if="scope.row.status === '进行中'" type="primary">{{ scope.row.status }}</el-tag>
            <el-tag v-else-if="scope.row.status === '已完成'" type="success">{{ scope.row.status }}</el-tag>
            <el-tag v-else-if="scope.row.status === '已取消'" type="danger">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="操作"
            width="150">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showEditTaskView(scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="deleteTask(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="display: flex;justify-content: flex-end;margin-top: 10px">
        <el-pagination
            background
            layout="sizes, prev, pager, next, jumper, ->, total"
            :total="total"
            :page-size="size"
            :current-page.sync="page"
            @current-change="initTasks"
            @size-change="sizeChange">
        </el-pagination>
      </div>
    </div>

    <!-- 添加任务对话框 -->
    <el-dialog
        title="添加任务"
        :visible.sync="dialogVisible"
        width="50%">
      <div>
        <el-form :model="taskForm" :rules="rules" ref="taskForm" label-width="100px">
          <el-form-item label="任务标题" prop="title">
            <el-input v-model="taskForm.title" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="任务内容" prop="content">
            <el-input type="textarea" v-model="taskForm.content" :rows="5" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="指派员工" prop="eid">
            <el-select v-model="taskForm.eid" style="width: 80%" placeholder="请选择员工">
              <el-option
                  v-for="item in employees"
                  :key="item.id"
                  :label="item.name + '(' + item.workID + ')'"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="截止时间" prop="endTime">
            <el-date-picker
                v-model="taskForm.endTime"
                type="datetime"
                placeholder="选择截止日期"
                style="width: 80%">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTask">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑任务对话框 -->
    <el-dialog
        title="编辑任务"
        :visible.sync="editDialogVisible"
        width="50%">
      <div>
        <el-form :model="updateForm" :rules="rules" ref="updateForm" label-width="100px">
          <el-form-item label="任务标题" prop="title">
            <el-input v-model="updateForm.title" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="任务内容" prop="content">
            <el-input type="textarea" v-model="updateForm.content" :rows="5" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="指派员工" prop="eid">
            <el-select v-model="updateForm.eid" style="width: 80%" placeholder="请选择员工">
              <el-option
                  v-for="item in employees"
                  :key="item.id"
                  :label="item.name + '(' + item.workID + ')'"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="截止时间" prop="endTime">
            <el-date-picker
                v-model="updateForm.endTime"
                type="datetime"
                placeholder="选择截止日期"
                style="width: 80%">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="任务状态">
            <el-select v-model="updateForm.status" style="width: 80%">
              <el-option label="未分配" value="未分配"></el-option>
              <el-option label="进行中" value="进行中"></el-option>
              <el-option label="已完成" value="已完成"></el-option>
              <el-option label="已取消" value="已取消"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateTask">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "TaskManage",
  data() {
    return {
      loading: false,
      tasks: [],
      total: 0,
      page: 1,
      size: 10,
      keyword: '',
      dialogVisible: false,
      editDialogVisible: false,
      taskForm: {
        title: '',
        content: '',
        eid: null,
        endTime: null
      },
      updateForm: {
        id: null,
        title: '',
        content: '',
        eid: null,
        endTime: null,
        status: ''
      },
      employees: [],
      rules: {
        title: [
          {required: true, message: '请输入任务标题', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入任务内容', trigger: 'blur'}
        ],
        eid: [
          {required: true, message: '请选择指派员工', trigger: 'change'}
        ],
        endTime: [
          {required: true, message: '请选择截止日期', trigger: 'change'}
        ]
      }
    }
  },
  mounted() {
    this.initTasks();
    this.initEmployees();
  },
  methods: {
    // 初始化任务列表
    initTasks() {
      this.loading = true;
      this.getRequest("/task/getAll/?page=" + this.page + "&size=" + this.size + "&keyword=" + this.keyword).then(resp => {
        this.loading = false;
        if (resp) {
          this.tasks = resp.data;
          this.total = resp.total;
        }
      });
    },

    // 初始化员工列表（用于选择指派）
    initEmployees() {
      this.getRequest("/employee/basic/").then(resp => {
        if (resp) {
          this.employees = resp.data;
        }
      });
    },

    // 页面大小改变
    sizeChange(size) {
      this.size = size;
      this.page = 1;
      this.initTasks();
    },

    // 显示添加任务对话框
    showAddTaskView() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        if (this.$refs.taskForm) {
          this.$refs.taskForm.resetFields();
        }
      });
    },

    // 显示编辑任务对话框
    showEditTaskView(data) {
      this.editDialogVisible = true;
      this.$nextTick(() => {
        this.updateForm.id = data.id;
        this.updateForm.title = data.title;
        this.updateForm.content = data.content;
        this.updateForm.eid = data.eid;
        this.updateForm.endTime = data.endTime;
        this.updateForm.status = data.status;
      });
    },

    // 添加任务
    addTask() {
      this.$refs.taskForm.validate(valid => {
        if (valid) {
          this.postRequest("/task/insert", this.taskForm).then(resp => {
            if (resp) {
              this.dialogVisible = false;
              this.initTasks();
              this.$message.success("添加成功!");
            }
          });
        }
      });
    },

    // 更新任务
    updateTask() {
      this.$refs.updateForm.validate(valid => {
        if (valid) {
          this.putRequest("/task/", this.updateForm).then(resp => {
            if (resp) {
              this.editDialogVisible = false;
              this.initTasks();
              this.$message.success("更新成功!");
            }
          });
        }
      });
    },

    // 删除任务
    deleteTask(data) {
      this.$confirm('此操作将删除该任务, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/task/" + data.id).then(resp => {
          if (resp) {
            this.initTasks();
          }
        });
      }).catch(() => {
        this.$message.info("已取消删除");
      });
    },
    // 导出任务数据
    exportData() {
      window.open('/task/export', '_parent');
    }
  }
}
</script>

<style scoped>

</style>
